Skip to content

Modify sdkstats manager#47363

Open
rads-1996 wants to merge 12 commits into
Azure:mainfrom
rads-1996:modify-sdkstats-manager
Open

Modify sdkstats manager#47363
rads-1996 wants to merge 12 commits into
Azure:mainfrom
rads-1996:modify-sdkstats-manager

Conversation

@rads-1996

@rads-1996 rads-1996 commented Jun 4, 2026

Copy link
Copy Markdown
Member

The corresponding PR in microsoft-opentelemetry - microsoft/opentelemetry-distro-python#191, currently the tests fail because they reference the add_metric_callback method which has been introduced in this PR.
Once the exporter is released and the latest version is pinned in the microsoft opentelemetry distro, we would be able to merge the PR.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Copilot AI review requested due to automatic review settings June 4, 2026 23:04
@github-actions github-actions Bot added the Monitor - Exporter Monitor OpenTelemetry Exporter label Jun 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the statsbeat system in azure-monitor-opentelemetry-exporter to allow SDKs/distros to register additional metric callbacks and have their observations included alongside the built-in statsbeat network metrics.

Changes:

  • Add a callback registry and an _iter_extra_observations helper to safely aggregate extra observations from registered callbacks.
  • Add StatsbeatManager.add_metric_callback for registering per-metric callbacks (idempotent per callback instance).
  • Update statsbeat network metric callbacks to append contributed observations, and add unit tests + changelog entry.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_utils.py Adds callback registry + iterator helper for contributed observations.
sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_manager.py Adds StatsbeatManager.add_metric_callback API for registering callbacks.
sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_statsbeat_metrics.py Appends extra observations to built-in network metrics callbacks.
sdk/monitor/azure-monitor-opentelemetry-exporter/tests/statsbeat/test_metrics.py Adds tests validating callback registration and observation aggregation behavior.
sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md Documents the new callback capability under Features Added.

@rads-1996 rads-1996 force-pushed the modify-sdkstats-manager branch 2 times, most recently from d128d6f to 15b56f1 Compare June 5, 2026 15:09
@rads-1996 rads-1996 force-pushed the modify-sdkstats-manager branch from 973cc6a to 79de717 Compare June 12, 2026 20:06
with self._lock:
return self._initialized

def add_metric_callback(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be invoked by the distro.

@rads-1996 rads-1996 force-pushed the modify-sdkstats-manager branch from 79de717 to 89679c5 Compare June 12, 2026 21:10
@rads-1996 rads-1996 force-pushed the modify-sdkstats-manager branch from 07aa1a0 to 2bd92fa Compare June 15, 2026 17:42
attributes["exceptionType"] = code
observations.append(Observation(int(count), dict(attributes)))
_REQUESTS_MAP[_REQ_EXCEPTION_NAME[1]][code] = 0 # type: ignore
observations.extend(_iter_additional_observations(_REQ_EXCEPTION_NAME[0], options))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yield pattern is good in _iter_additional_observations but observations.extend materializes all yielded items anyways, effectively cancelling out the optimization. You can jus return instead of yield to be consistent.

@@ -0,0 +1,189 @@
```py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

@rads-1996 rads-1996 Jun 15, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting some api inconsistency error in the CI/CD pipeline, trying to fix that. EDIT: So, I investigated the cause of this issue and looks like this PR - #47203 adds this new API consistency check which requires us to add the api.md and the metadata file. I believe this should be a one-time occurrence, unless the API is updated and we will have to push that change then.


- Add `StatsbeatManager.add_metric_callback` to let SDKs/distros add their own metric
observations to built-in statsbeat metrics
([#47363](https://github.com/Azure/azure-sdk-for-python/pull/47363))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: blank line

:rtype: bool
"""
with _ADDITIONAL_CALLBACKS_LOCK:
callbacks = _ADDITIONAL_CALLBACKS.setdefault(metric_name, [])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add additional callbacks globals to state and have gettors / settors pattern just like the other global variables.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can then also make the additional callbacks as fields on the manager instead of accessing it directly from globals.

@rads-1996 rads-1996 Jun 15, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can then also make the additional callbacks as fields on the manager instead of accessing it directly from globals.

@lzchen What do you mean by "make additional callbacks as fields on the manager"? Could you please elaborate on that.

iter_logger = logging.getLogger(__name__)
for cb in callbacks:
try:
yield from cb(options)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is passing in options for future proofing? I don't think our internal callbacks use them right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Monitor - Exporter Monitor OpenTelemetry Exporter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants